
<template>
  <div  class="app-container">
    <!-- 搜索筛选 -->
    <el-form :inline="true"  class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="search" placeholder="输入标签"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="large" type="success" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>

      <el-table-column prop="tagId" label="标签id" >
      </el-table-column>

      <el-table-column prop="tagName" label="tag名称" >
      </el-table-column>
      <el-table-column label="tag创建时间" sortable>
        <template slot-scope="scope">
          {{scope.row.tagCreateTime| dateFormat }}
        </template>
      </el-table-column>

      <el-table-column prop="tagInfo" label="tag简介">
      </el-table-column>


      <el-table-column align="center" label="操作" >
        <template slot-scope="scope">
          <el-button size="mini" icon="el-icon-edit" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" icon="el-icon-delete" type="danger" @click="deleteTag(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="40%" @click="closeDialog">
      <!-- 可以修改标题和简介-->
      <el-form label-width="120px" :model="editForm" ref="editForm">
        <el-form-item label="tag标题" prop="tagName">
          <el-input size="small" v-model="editForm.tagName" auto-complete="off" placeholder="请输入标签名"></el-input>
        </el-form-item>
        <el-form-item label="tag简介" prop="tagInfo">
          <el-input size="small" v-model="editForm.tagInfo" auto-complete="off" placeholder="请输入标签简介"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitUpdateForm('editForm')">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


import {tagAdd, tagDelete, tagSave, tagsGet} from "@/api/tag";

export default {
  data() {
    return {
      search: '',
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      editForm: {
        tagId: '',
        tagInfo: '',
        tagName: '',
      },
      // rules表单验证
      rules: {
        // deptName: [
        //   { required: true, message: '请输入blog名称', trigger: 'blur' }
        // ],
        // deptNo: [{ required: true, message: '请输入blog代码', trigger: 'blur' }]
      },
      listData: [], //用户数据
    }
  },
  // 注册组件
  components: {
  },
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  created() {
    this.getTagList()
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    getTagList(){
      tagsGet().then(res => {
        this.listData = res.data
        }
      ).catch(err => {
          console.error(err);
        }
      )
    }
    ,
    // 搜索事件
    search() {
      this.getdata(this.formInline)
    },
    //显示编辑界面
    handleEdit: function(index, row) {
      this.editFormVisible = true
      //判断是否是编辑，即已经有的数据
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        this.editForm.tagInfo = row.tagInfo
        this.editForm.tagId = row.tagId
        this.editForm.tagName = row.tagName
      } else {
        this.title = '添加'
        this.editForm.tagInfo = ''
        this.editForm.tagId = ''
        this.editForm.tagName = ''
      }
    },
    // 编辑 保存方法
    submitUpdateForm() {
          //判断为添加还是更新
          if (this.editForm.tagId===''){



            //添加
            tagAdd(this.editForm)
              .then(res => {
                this.editFormVisible = false
                this.loading = false
                if (res.code===0) {
                  this.$message({
                    type: 'success',
                    message: 'tag保存成功！'
                  })

                } else {
                  this.$message({
                    type: 'info',
                    message: res.msg
                  })
                }
                this.getTagList()  /*刷新一下*/
              })
              .catch(err => {
                this.editFormVisible = false
                this.loading = false
                this.$message.error('tag保存失败，请稍后再试！'+err)
              })


          }else {


            //更新
            tagSave(this.editForm)
              .then(res => {
                this.editFormVisible = false
                this.loading = false
                if (res.code===0) {
                  this.$message({
                    type: 'success',
                    message: 'tag保存成功！'
                  })

                } else {
                  this.$message({
                    type: 'info',
                    message: res.msg
                  })
                }
                this.getTagList()  /*刷新一下*/
              })
              .catch(err => {
                this.editFormVisible = false
                this.loading = false
                this.$message.error('tag保存失败，请稍后再试！')
              })

      }
    },
    // 删除tag
    deleteTag(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          tagDelete(row.tagId)
            .then(res => {
              if (res.code===0) {
                this.$message({
                  type: 'success',
                  message: 'tag已删除!'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
              this.getTagList()
            })
            .catch(err => {
              this.loading = false
              this.$message.error('tag删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>


